#include "TimerEvent.h"

TimerEvent::TimerEvent(const ticker_data_t *data) : event(), _ticker_data(data)
{
    _ticker_data.set_handler(irq);
}

void TimerEvent::irq(uint32_t id)
{
    TimerEvent *timer_event = reinterpret_cast<TimerEvent *>(id);
    timer_event->handler();
}

TimerEvent::~TimerEvent()
{
    remove();
}

void TimerEvent::insert(timestamp_t timestamp)
{
    ticker_insert_event(_ticker_data, &event, timestamp, (uint32_t)this);
}

void TimerEvent::insert(us_timestamp_t rel_time)
{
    insert_absolute(_ticker_data.now() + rel_time);
}

void TimerEvent::insert_absolute(us_timestamp_t timestamp)
{
    ticker_insert_event_us(_ticker_data, &event, timestamp, (uint32_t)this);
}

void TimerEvent::remove()
{
    _ticker_data.remove_event(&event);
}